﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using POS.Framework;
using DevExpress.Xpo;
using Model.Bill;
using BLL;
using DevExpress.Data.Filtering;
using BLL.Enums;

namespace POS.ModForm.Bill
{
    public partial class frmAdjustPrice : DevExpress.XtraEditors.XtraForm
    {
        public frmAdjustPrice()
        {
            InitializeComponent();
        }

        private void frmAdjustPrice_Load(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                this.gridView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                this.repositoryItemLookUpEditStatus.DataSource = EnumManage.GetDictionaryEntry(typeof(AdjustPriceStatus));
                SetButtonClickEvent();
                SearchData();
            }
        }
        void SetButtonClickEvent()
        {
            this.listButtonUC.Query.Click += new EventHandler(Query_Click);
            this.listButtonUC.New.Click += gridView_RowClick;
            this.gridView.RowClick += gridView_RowClick;
            this.gridView.KeyPress += gridView_RowClick;
        }
        void gridView_RowClick(object sender, EventArgs e)
        {
            object Id=string.Empty;
            if (!SpecialGridView.IsSimpleButton(sender))
            {
                if (this.gridView.FocusedRowHandle < 0) return;
                Id = this.gridView.GetRowCellValue(this.gridView.FocusedRowHandle, "AdjustPriceID");
            }
            FormFactory.OpenForm("POS.ModForm.Bill.frmAdjustPriceForm", "调价单编辑", Id, this.MdiParent);
        }
        void Query_Click(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                SearchData();
                wdf.Close();
            }
        }
        void SearchData()
        {
            GroupOperator goFilter = new GroupOperator();
            if (!string.IsNullOrEmpty(this._CreateDTStart.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("CreateDT>=?", this._CreateDTStart.Text.Trim()));
            if (!string.IsNullOrEmpty(this._CreateDTEnd.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("CreateDT<?", this._CreateDTEnd.Text.Trim()));
            if (!string.IsNullOrEmpty(this._AuditDateStart.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("AuditDate>=?", this._AuditDateStart.Text.Trim()));
            if (!string.IsNullOrEmpty(this._AuditDateEnd.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("AuditDate<?", this._AuditDateEnd.Text.Trim()));
            if (!string.IsNullOrEmpty(this._BillCode.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("BillCode=?", this._BillCode.Text.Trim()));

            XPCollection<TB_AdjustPrice> sd = new DevExpress.Xpo.XPCollection<TB_AdjustPrice>(DatabaseSchema.Session, goFilter);
            this.gridControl.DataSource = sd;
            SpecialGridView.SetGridViewIndicatorWidth(sd.Count, this.gridView);
        }
    }
}